package org.study.enums;

import lombok.Getter;

/**
 * 枚举类在实际项目中的应用示例：权限系统
 * 参考 https://blog.csdn.net/MeyrlNotFound/article/details/147870300
 *
 * @author doveylovey
 * @version v1.0.0
 * @email 1135782208@qq.com
 * @date 2025年07月16日
 */
public class PermissionEnumTest {
    public static void main(String[] args) {

    }

    @Getter
    public enum Permission {
        // 0001
        READ(1),
        // 0010
        WRITE(2),
        // 0100
        EXECUTE(4),
        // 1000
        DELETE(8);

        private final int mask;

        Permission(int mask) {
            this.mask = mask;
        }

        public static int combine(Permission... permissions) {
            int result = 0;
            for (Permission p : permissions) {
                result |= p.mask;
            }
            return result;
        }

        public static boolean hasPermission(int permissions, Permission permission) {
            return (permissions & permission.mask) != 0;
        }
    }
}
